""""""
"""
______________________________
time：2021/5/28
封装一个类，用来读取xx.yaml格式的配置文件里的数据，以及向配置文件中写入数据；
注：读取yaml文件中的数据，yaml的一大好处就是会帮我们自动转换数据的类型，int，boolean等，更方便
______________________________

"""
import os
import yaml

from scripts.handle_path import CONFIGS_FILE_PATH, CONFIGS_DIR


class HandleYaml(object):
    def __init__(self, filename):
        self.filename = filename

    def read_yaml(self, section, option):
        """读取配置文件xxx.yaml中的信息"""
        # 读取yaml格式的文件
        with open(self.filename, encoding='utf8') as one_file:
            self.datas = yaml.load(one_file, Loader=yaml.FullLoader)
        return self.datas[section][option]

    def write_yaml(self, datas):
        """写入信息到配置文件xxx.yaml中"""
        # 写入数据到yaml格式的文件
        with open(self.filename, mode="w",
                  encoding='utf8') as one_file:
            yaml.dump(datas, one_file, allow_unicode=True)  # allow_unicode不将中文进行编码


"""使用import os，优化路径"""
# do_yaml = HandleYaml(r"D:\Pycharm\APITestProject\configs\xbb_testcase.yaml")
do_yaml = HandleYaml(CONFIGS_FILE_PATH)

# if __name__ == '__main__':
#     pass
    # res = do_yaml.read_yaml('msg', 'success_result')
    # print(res)
    # write_yaml_path = os.path.join(CONFIGS_DIR, 'write_into_testcase.yaml')
    # hd_yaml = HandleYaml(write_yaml_path)
    # # 写入配置中的数据
    # datas = {
    #     "excel":
    #         {"cases_path": " cases.xlsx"},
    #     "user":
    #         {"username": "wwwwwwwwwwwwwwwwwww",
    #          "password": "123456"
    #          },
    #     "book": {
    #         "bookname": "怪诞心理学",
    #         "bookauthor": "不知道"
    #     }
    # }
    # hd_yaml.write_yaml(datas)
